﻿using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using AvaloniaMVVMSample.Models;
using DynamicData;
using ReactiveUI;

namespace AvaloniaMVVMSample.ViewModels;

public class MainWindowViewModel : ViewModelBase
{
    private SystemModel? _model;
    public SystemModel? Model
    {
        get => _model;
        set
        {
            this.RaiseAndSetIfChanged(ref _model, value);
            if(value == null)return;
            Text = value.Path;
        }
    }
    
    private string _text = "";

    public string Text
    {
        get => _text;
        set
        {
            if (string.IsNullOrEmpty(value)) return;
            if (!Directory.Exists(value))
            {
                if (!File.Exists(value)) return;
                var file = new FileModel(value);
                file.Open();
                return;
            }
            this.RaiseAndSetIfChanged(ref _text, value);
            var folder = new DirectoryInfo(value);
            List.Clear();
            List.Add(folder.GetFileSystemInfos().Where(x =>
                (x.Attributes & FileAttributes.System) != FileAttributes.System &&
                (x.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden).Select(x => x.GetSys()));
        }
    }

    public ObservableCollection<SystemModel> List { get; set; } = [];

    public MainWindowViewModel()
    {
        Text = DriveInfo.GetDrives()[0].Name;
    }
}